package templatejbdc;



import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
//把我们要操作的功能封装成方法
public class MyTemplate {

    //使用JdbcTemplate注入的是数据源
    private JdbcTemplate template = new JdbcTemplate(JdbcUtils.getDataSource());

    /**
     *  //查询所有学生
     * @return
     * @throws Exception
     */
    public List<Student> findAll() throws Exception {
        //2.写sql语句(查询所有学生)
        String sql = "SELECT * FROM student";
        List<Student> list = template.query(sql,new BeanPropertyRowMapper(Student.class));
        return list;
    }

    /**
     * 按名字查询
     * @param name
     * @return
     */
    public Student findByName(String name){
        String sql = "SELECT * FROM student where name= ?";
        Student student =template.queryForObject(sql,new BeanPropertyRowMapper<>(Student.class),name);
        return student;
    }

    /**
     *  添加一条记录
     */
    public int add(Student student){
        String sql = "INSERT INTO student(name,age,gander,address,birthday) VALUES (?,?,?,?,?)";
        Object[] args = {student.getName(),student.getAge(),student.getGander(),student.getAddress(),null};
        int result = template.update(sql,args);
        return result;
    }

    /**
     *
     * @param student
     * @return
     */
    public int update(Student student){
        return 0;
    }

    public int delete(int id){
        return 0;
    }
    //批量删除
}
